package game.square;


import java.util.*;

import be.kuleuven.cs.som.annotate.*;

/**
 * A class of doors used as a special type of a border.
  
 * @version 1.0
 * @author Chris Desomer, Geert Heyman
 */
public class Door extends OpenableBorder {

	/**
	 * Initialize this new door
	 * 
	 * @effect	Initialize this new door as a new openable border
	 *		  | super(false)
	 */
	@Raw
	public Door()
			throws IllegalArgumentException, IllegalStateException {
		super(false); // the door is set closed as default value
	}
	
	/**
	 * Check if this door is open or not.
	 * 
	 */
	@Basic
	public boolean isOpen(){
		return open;
	}
	
	/**
	 * Open this door.
	 * 
	 * @post 	...
	 * 	   	  | isOpen()
	 * @effect	...
	 * 		  | if (getBorderedSquares()[1] != null && getBorderedSquares()[2] != null))
	 * 		  |		then new.getBorderedSquares()[1].setRoomEquilibrium(getBorderedSquares()[1]
	 * 		  |			.getRoom(), getBorderedSquares()[2].getRoom()) 
	 */
	public void open(){
		Set<RoomSquare> onePartOfRoom = new HashSet<RoomSquare>();
		Set<RoomSquare> otherPartOfRoom =  new HashSet<RoomSquare>();
		if (getBorderedSquares()[1] != null && getBorderedSquares()[2] != null){	// a square with a door as border, must be a room square.
			onePartOfRoom = ((RoomSquare)getBorderedSquares()[1]).getRoom();
			otherPartOfRoom = ((RoomSquare) getBorderedSquares()[2]).getRoom();
		}
		open = true;
		if (getBorderedSquares()[1] != null && getBorderedSquares()[2] != null)
			((TeleportationSquare)getBorderedSquares()[1]).setRoomEquilibrium(onePartOfRoom, otherPartOfRoom);
	}
	
	/**
	 * Close this door.
	 * 
	 *  @post	...
	 *  	  | !isOpen()
	 */
	public void close(){
		open = false;
	}
	
	/**
	 * Variable that stores whether or not this door is open.
	 */
	private boolean open;
	
	/**
	 * Check if this border is an open border.
	 * 
	 * @return	...
	 * 		  | result == isOpen()
	 */
	@Override
	public boolean isOpenBorder(){
		return isOpen();
	}
	
	/**
	 * Get newly created border (it borders no squares), that is
	 * on all other aspects equal to this border.
	 *  
	 * @return	result.isCreated() && result.getClass() == getClass()
	 * 			&& result.isOpen() == isOpen()
	 */
	@Override
	public Border getNewEqualBorder(){
		Door door = new Door();
		if (isOpen())
			door.open();
		return door;
	}
	
}
